<template>
    <div id="login">
        <mt-field label="用户名" placeholder="请输入用户名" v-model="username"></mt-field>
        <mt-field label="密码" placeholder="请输入密码" type="password" v-model="password"></mt-field>
        <mt-field label="验证码" v-model="code">
            <img ref="imgUrl" @click="changeUrl" :src="$url.yzm" height="25px" width="100px" />
        </mt-field>
        <mt-button @click="login" type="primary" size="large">登录</mt-button>
    </div>
</template>

<script>
import { Toast } from 'mint-ui';
export default {
    name: "login",
    data() {
        return {
            // hyl001
            username: "",
            password: "",
            code:"",
            n:1
        };
    },
    methods:{
        changeUrl(){
            this.n++
            this.$refs.imgUrl.src =this.$url.yzm +"?id="+this.n
        },
        login(){
            this.$api.post(this.$url.login,{
                account:this.username,
                password:this.password,
                code:this.code
            },res=>{
                if(res.data.code == 200){
                    Toast('登录成功！')
                    this.water();
                    this.$store.commit('getUser',JSON.stringify(res.data.data))
                    this.$router.push({path:'/personal'})
                }
            })
        },
        water() {
            // var waterList = [];
            // var num = 0.0;
            // for (var i = 0; i < 100; i++) {
            //     num = this.addFun(num, 0.001);
            //     waterList.push(num.toFixed(3));
            // }
            // this.$store.commit("getCut", JSON.stringify(waterList));
            var waterList = [];
            var num = -0.0001;
            for (var i = 0; i <= 100; i++) {
                num = this.addFun(num, 0.0001);
                waterList.push(num.toFixed(4));
            }
            this.$store.commit("getCut", JSON.stringify(waterList));
        },
        addFun(arg1, arg2) {
            var r1, r2, m;
            try {
                r1 = arg1.toString().split(".")[1].length;
            } catch (e) {
                r1 = 0;
            }
            try {
                r2 = arg2.toString().split(".")[1].length;
            } catch (e) {
                r2 = 0;
            }
            m = Math.pow(10, Math.max(r1, r2));
            return (arg1 * m + arg2 * m) / m;
        }
    }
};
</script>

<style lang='less' scoped>
.mint-field {
    border-bottom: 1px solid @color;
}
#login{
    padding: 10vh 10px 10px 10px;
    box-sizing: border-box;  
}
</style>
